import { ChangeDetectorRef, Component, ViewChild, ViewRef, OnInit } from '@angular/core';
import { STColumn, STComponent, STData, STPage, STRes } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { ModalHelperOptions } from '@delon/theme/src/services/modal/modal.helper';
import * as moment from 'moment';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { zip } from 'rxjs';
import { finalize } from 'rxjs/operators';

import { BusinessOrderSetPurchaseEditComponent } from '../set-purchase-edit/set-purchase-edit.component';

@Component({
  selector: 'app-business-order-set-purchase',
  templateUrl: './set-purchase.component.html'
})
export class BusinessOrderSetPurchaseComponent implements OnInit {
  i: any;
  itemCurrent: any;
  purchase: any;
  purchase_types: any;

  public st!: this;
  //@ViewChild('purchaseTable') private readonly st!: BusinessOrderSetPurchaseComponent;
  //@ViewChild('BusinessOrderSetPurchaseEditComponent') private readonly st2!: BusinessOrderSetPurchaseComponent;

  setDefaultStatus = {
    '0': { index: 0, text: '设为默认', notice: '设为默认', tooltip: '点击设为默认', color: '#36CFC9' },
    '1': { index: 1, text: '已为默认', notice: '已为默认', tooltip: '点击取消默认', color: 'blue' }
  };

  constructor(
    private modal: NzModalRef,
    private modal2: ModalHelper,
    private modal3: NzModalService,
    private msgSrv: NzMessageService,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    this.http.get(`api/purchase_types`).subscribe(res => {
      this.purchase_types = res.data;
      console.log('purchase_types', this.purchase_types);
    });
    if (this.i.orderSn != '') {
      this.loadDataFromServer(this.i.shopId, this.i.orderSn, this.i.itemId, this.i.skuId);
    }
  }

  loadDataFromServer(shopId: number, orderSn: string, itemId: string, skuId: string) {
    zip(
      this.http.get(`api/order/item_current`, { shopId: shopId, orderSn: orderSn }),
      this.http.get(`api/purchases`, { shopId: shopId, orderSn: orderSn, itemId: itemId, skuId: skuId })
    )
      .pipe(
        finalize(() => {
          this.cdr.detectChanges();
        })
      )
      .subscribe(([itemCurrent, purchases]: [NzSafeAny, NzSafeAny]) => {
        // this.i = true;
        console.log(purchases);
        this.itemCurrent = itemCurrent.data;
        this.purchase = purchases?.data;
      });
  }

  //添加
  add(shopId: number, orderSn: string, orderId: string, itemId: string, skuId: string): void {
    console.log(shopId, orderSn, itemId, skuId);
    console.log('purchase_types', this.purchase_types);
    this.modal2
      .createStatic(BusinessOrderSetPurchaseEditComponent, {
        purchase_types: this.purchase_types,
        purchase: this.purchase,
        parent_cdr: this.cdr,
        i: { id: 0, shopId, orderSn, orderId, itemId, skuId }
      })
      .subscribe(() => {
        //this.st?.reload();
        this.reload();
      });
  }

  //编辑
  edit(record: STData, shopId: number, orderSn: string, orderId: string, itemId: string, skuId: string): void {
    //console.log(shopId, orderSn, itemId, skuId, record);
    //console.log(this.purchase);
    //modal2: ModalHelper
    this.modal2
      .createStatic(BusinessOrderSetPurchaseEditComponent, {
        record: record,
        purchase_types: this.purchase_types,
        purchase: this.purchase,
        parent_cdr: this.cdr,
        i: { shopId, orderSn, orderId, itemId, skuId }
      })
      .subscribe(() => {
        //this.st?.reload();
        this.reload();
      });

    //this.modal3.create();
    this.modal3.afterAllClose.subscribe(md => {
      console.log('----');
    });
  }

  del(id: number): void {
    if (confirm('是否删除该条信息？')) {
      this.http.delete(`api/purchases/${id}`).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.msgSrv.success(res.msg);
        //this.st?.reload();
        this.reload();
      });
    }
  }

  reload(): void {
    this.cdr.detectChanges();
    this.http
      .get(`api/purchases`, { shopId: this.i.shopId, orderSn: this.i.orderSn, itemId: this.i.itemId, skuId: this.i.skuId })
      .subscribe(res => {
        this.cdr.detectChanges();
        this.purchase = res?.data;
      });
  }

  //修改设为默认
  changeDefault(id: number, isDefault: number): void {
    console.log(id);
    console.log(isDefault);
    this.http.put(`api/purchase_change/${id}/${isDefault}`).subscribe(res => {
      if (res.err_code != 0) {
        this.msgSrv.error(res.msg);
        return;
      }
      this.msgSrv.success(res.msg);
      //this.st?.reload();
      this.reload();
    });

    //this.st?.reload();
    this.reload();
  }

  close(): void {
    this.modal.destroy();
  }
}
